package com.violet.user.server.user.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.violet.common.entity.User;
import com.violet.common.page.PageParam;
import com.violet.common.page.PageResult;
import com.violet.common.util.ObjectCopyUtil;
import com.violet.db.util.PageUtils;
import com.violet.redis.anno.CacheDel;
import com.violet.user.api.user.dto.UserDTO;
import com.violet.user.server.user.condition.UserCondition;
import com.violet.user.server.user.mapper.UserMapper;
import com.violet.user.server.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: wqf
 * @Date: 2023/06/13
 * @Description:
 */
@Slf4j
@Component
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 方法描述: 查询用户信息列表
     *
     * @param userCondition 用户信息列表查询入参
     * @param pageParam     分页条件
     * @return java.util.List<com.voilet.user.server.use.domain.User>
     * @author wqf
     * @date 2023/6/13 17:50
     */
    @CacheDel(keyPrefix = "u:info", keyValue = "{0}")
    @Override
    public PageResult<User> listUser(UserCondition userCondition, PageParam pageParam) {
        log.info("获取用户信息列表查询入参========{}，{}", JSONUtil.toJsonStr(userCondition), JSONUtil.toJsonStr(pageParam));
        Page<User> page = PageUtils.getPage(pageParam);
        Page<User> userPage = userMapper.selectPage(page, new LambdaQueryWrapper<>());
        log.info("获取用户信息列表查询结果========{}", userPage.getRecords().size());
        return PageUtils.convertPageResult(userPage);
    }

    /**
     * 方法描述: 根据用户名称查询用户信息
     *
     * @param username 用户名称
     * @return com.voilet.user.server.use.dto.UserDTO
     * @author wqf
     * @date 2023/6/14 9:32
     */
    @Override
    public UserDTO getUserByUserName(String username) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        return ObjectCopyUtil.copyProperties(user, UserDTO::new);
    }
}
